#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>

struct ReturnType {
    std::vector<int> result;
    bool isUnique;
    bool hasCycle;
    ReturnType(std::vector<int> r, bool u, bool h) : result(r), isUnique(u), hasCycle(h) {}
};

ReturnType TopoSort(int n, const std::vector<std::vector<int>>& graph, std::vector<int> indegree, const std::unordered_set<int>& charSet) {
    std::vector<int> result;
    bool isUnique = true;
    int charCount = charSet.size();

    std::queue<int> zeroDegree;
    for (int i = 0; i < indegree.size(); i++) {
        if (indegree[i] == 0 && charSet.find(i) != charSet.end()) {
            zeroDegree.push(i);
        }
    }

    while (!zeroDegree.empty()) {
        if (zeroDegree.size() > 1) {
            isUnique = false;
        }
        int me = zeroDegree.front();
        zeroDegree.pop();
        result.push_back(me);

        for (int neighbor : graph[me]) {
            if (--indegree[neighbor] == 0 && charSet.find(neighbor) != charSet.end()) {
                zeroDegree.push(neighbor);
            }
        }
    }

    bool hasCycle = (result.size() != charCount);
    return {result, isUnique, hasCycle};
}

/**
 * 排序
 */
int main() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<int>> graph(n, std::vector<int>());
    std::vector<int> indegree(n, 0);
    std::unordered_set<int> charSet;

    for (int i = 1; i <= m; i++) {
        char a, op, b;
        std::cin >> a >> op >> b;

        int u = a - 'A';
        int v = b - 'A';

        graph[u].push_back(v);
        indegree[v]++;
        charSet.insert(u);
        charSet.insert(v);

        ReturnType rt = TopoSort(n, graph, indegree, charSet);

        if (rt.hasCycle) {
            std::cout << "Inconsistency found after " << i << " relations.\n";
            return 0;
        }

        if (charSet.size() == n && rt.result.size() == n && rt.isUnique) {
            std::cout << "Sorted sequence determined after " << i << " relations: ";
            for (int c : rt.result) {
                std::cout << (char)(c + 'A');
            }
            std::cout << ".\n";
            return 0;
        }
    }

    ReturnType rt = TopoSort(n, graph, indegree, charSet);
    if (rt.hasCycle) {
        std::cout << "Inconsistency found after " << m << " relations.\n";
    }
    else {
        if (charSet.size() == n && rt.result.size() == n && rt.isUnique) {
            std::cout << "Sorted sequence determined after " << m << " relations: ";
            for (int c : rt.result) {
                std::cout << (char)(c + 'A');
            }
            std::cout << ".\n";
        }
        else {
            std::cout << "Sorted sequence cannot be determined.\n";
        }
    }

    return 0;
}